package com.city.ucenterservice.controller;


import com.city.commonutils.JwtUtils;
import com.city.commonutils.Result;
import com.city.ucenterservice.entity.UcenterMember;
import com.city.ucenterservice.entity.vo.RegisteredVo;
import com.city.ucenterservice.service.UcenterMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author city
 * @since 2020-10-01
 */
@RestController
@RequestMapping("/ucenterservice/member")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService ucenterMemberService;

    //GetMapping不能通过requestbody接受参数
    @PostMapping("login")
    public Result login(@RequestBody UcenterMember ucenterMember){
        //登录后使用jwt生成token值通过地址返回给前端
        String token=ucenterMemberService.login(ucenterMember);
        return Result.ok().data("token",token);
    }

    @PostMapping("register")
    public Result register(@RequestBody RegisteredVo registeredVo){
        ucenterMemberService.register(registeredVo);
        return Result.ok();
    }

    //根据token获取用户信息，用户携带token访问网页，后台判断是否登录，和返回信息，实现多模块登录
    @GetMapping("getMemberInfo")
    public Result getMemberInfo(HttpServletRequest request){
        //登录后返回token给前端，前端放入cookie的head中，后端通过rqquest获取请求头等信息，拿到用户token解码后判断用户，查询信息返回前端显示
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember byId = ucenterMemberService.getById(memberIdByJwtToken);
        return Result.ok().data("userInfo",byId);
    }

    @GetMapping("countRegister/{day}")
    public Result countRegister(@PathVariable String day){
        int count=ucenterMemberService.getCountRegister(day);
        return Result.ok().data("count",count);
    }
}